package dao;

import db.DbUtil;
import entity.Cart;
import entity.CartItem;
import entity.User;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author 蒋银辉
 * @version 1.0
 */
public class CartDao {

    public int addToCart(Cart mycart) {
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String createTime = currentTime.format(formatter);
        int ret = DbUtil.getInstance().execute("insert into cart_info(user_id,goods_id,type_id,goods_name,goods_image,goods_price,qty,cart_sum,note,create_time,update_time) values(?,?,?,?,?,?,?,?,?,?,?)", new Object[]{mycart.getUserId(),mycart.getGoodsId(),mycart.getTypeId(),mycart.getGoodsName(),mycart.getGoodsImage(),mycart.getGoodsPrice(),mycart.getQty(),mycart.getCartSum(),mycart.getNote(),createTime,createTime});
        return ret;
    }

    public Cart selectByCartId(String cartId) {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = getConnection();
            stmt = conn.createStatement();
            String sql = "select * from cart_info where cart_id=" + cartId;
            ResultSet rs = stmt.executeQuery(sql);
            Cart cartItem = null;
            if(rs.next()) {
                cartItem = new Cart();
                cartItem.setCartId(rs.getInt("cart_id"));
                cartItem.setGoodsId(rs.getInt("goods_id"));
                cartItem.setGoodsName(rs.getString("goods_name"));
                cartItem.setGoodsImage(rs.getString("goods_image"));
                cartItem.setGoodsPrice(rs.getBigDecimal("goods_price"));
                cartItem.setNote(rs.getString("note"));
                cartItem.setQty(rs.getInt("qty"));
                cartItem.setCartSum(rs.getBigDecimal("cart_sum"));
            }
            return cartItem;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeConnection(stmt, conn);
        }
        return null;
    }

    public List<Cart> selectMyCart(Integer userId) {
        List<Cart> list=DbUtil.getInstance().queryList("select * from cart_info where user_id=?",new Object[] {userId},Cart.class);
        return list;
    }

    public int deleteByIdCart(Integer id) {
        int ret = DbUtil.getInstance().execute("delete from cart_info where cart_id=?", new Object[]{id});
        return ret;
    }

    private Connection getConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=utf8",
                    "root", "123456");
            return conn;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    private void closeConnection(Statement stmt, Connection conn) {
        try {
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}